<template>
	<view class="container" style="padding-bottom: 120rpx;">
		<u--form ref="queryForm" :model="query" labelWidth="120rpx">
			<u-form-item label="料架码" prop="rackCode">
				<u--input
					placeholder="请输入/扫描" 
					v-model="query.rackCode" 
					suffixIcon="scan"
					suffixIconStyle="font-size: 68rpx"
					style="flex: 1;"
					@confirm="queryQualityList"
				>
				</u--input>
			</u-form-item>
			<u-form-item label="ReelId" prop="reelId">
				<u--input 
					placeholder="请输入/扫描" 
					v-model="query.reelId" 
					suffixIcon="scan" 
					suffixIconStyle="font-size: 68rpx"
					style="flex: 1;"
					@confirm="extraInfo"
				>
				</u--input>
			</u-form-item>
		</u--form>
		<view style="width: 100%;text-align: center;font-weight: 600;margin-top: 20rpx;">待抽检物料明细</view>
		<uni-table ref="info" border stripe emptyText="暂无数据" style="margin: 10px 0">
			<uni-tr>
				<uni-th width="100">物料编号</uni-th>
				<!-- <uni-th width="30">批次号</uni-th> -->
				<uni-th width="100">质检数量</uni-th>
				<uni-th width="100">所在位置</uni-th>
			</uni-tr>
			<uni-tr v-for="(item, index) in materialList" :key="index" class="td-container">
				<uni-td width="100">
					<view @click="checkDetail(item.materialCode)">
						{{ item.materialCode }}
					</view>
				</uni-td>
				<!-- <uni-td>{{ item.materialBatchNo }}</uni-td> -->
				<uni-td width="100">{{ item.materialQty }}</uni-td>
				<uni-td width="100">{{ item.slotCode }}</uni-td>
			</uni-tr>
		</uni-table>
		<!-- 模态框 -->
		<u-modal
			:show="show"
			:title="title"
			@confirm="cancelModal"
		>
			<view style="display: flex;flex-direction: column;">
				<view style="display: flex;">
					<view style="flex: 3;">物料编号：</view>
					<view style="flex: 3;">{{ selectedMaterial.materialCode }}</view>
				</view>
				<view style="display: flex;">
					<view style="flex: 3;">容器编码：</view>
					<view style="flex: 3;">{{ selectedMaterial.containerCode }}</view>
				</view>
				<view style="display: flex;">
					<view style="flex: 3;">批次号：</view>
					<view style="flex: 3;">{{ selectedMaterial.materialBatchNo }}</view>
				</view>
				<view style="display: flex;">
					<view style="flex: 3;">质检数量：</view>
					<view style="flex: 3;">{{ selectedMaterial.materialQty }}</view>
				</view>
				<view style="display: flex;">
					<view style="flex: 3;">单位：</view>
					<view style="flex: 3;">{{ selectedMaterial.unit }}</view>
				</view>
			</view>
		</u-modal>
		<!-- 提示组件 -->
		<u-toast ref="uToast"></u-toast>
		<view class="commonButton">
		  <u-button type="warning" :disabled="Object.keys(scanMaterial).length === 0" @click="qualityUnbind">抽料解绑</u-button>
		  <u-button type="primary" @click="qualityFinish">质检完成</u-button>
		</view>
	</view>
</template>

<script>
	import { queryQualityListAPI, unBindAPI, completeQualityAPI } from '@/api/work/inbound'
	import {
		uniCard,
		uniTable,
		uniTr,
		uniTd,
		uniTh
	} from '@dcloudio/uni-ui'
	export default {
		components: {
			uniCard,
			uniTable,
			uniTr,
			uniTd,
			uniTh
		},
		data() {
			return {
				query: {
					rackCode: '',
					reelId: ''
				},
				selectedMaterial: {},
				show: false,
				title: '物料详情',
				materialList: [],
				scanMaterial: {}
			}
		},
		methods: {
			async queryQualityList() {
				const submitForm = {
					rackCode: this.query.rackCode
				}
				const res = await queryQualityListAPI(submitForm)
				// console.log(res)
				if (res.code === 200) {
					this.materialList = res.data || []
				}
			},
			async qualityUnbind() {
				const submitForm = {
					labelNo: this.query.reelId
				}
				const res = await unBindAPI(submitForm)
				// console.log(res)
				if (res.code === 200) {
					this.$refs.uToast.show({
						type: 'success',
						message: res.message
					})
					this.query.reelId = ''
					this.scanMaterial = {}
					this.queryQualityList()
				} else {
					return this.$refs.uToast.show({
						type: 'warning',
						message: res.message
					})
				}
			},
			async qualityFinish() {
				const submitForm = {
					rackCode: this.query.rackCode
				}
				const res = await completeQualityAPI(submitForm)
				if (res.code === 200) {
					// console.log(res)
					this.query = {
						rackCode: '',
						reelId: ''
					}
					this.$refs.uToast.show({
						type: 'success',
						message: res.message
					})
				} else {
					return this.$refs.uToast.show({
						type: 'warning',
						message: res.message
					})
				}
			},
			checkDetail(val) {
				const foundMaterial = this.materialList.find(item => item.materialCode === val)
				if (foundMaterial) {
					this.selectedMaterial = foundMaterial
					this.show = true
				}
			},
			cancelModal() {
				this.show = false
			},
			extraInfo() {
				if (!this.query.reelId.includes('*')) {
					this.query.reelId = ''
					return this.$refs.uToast.show({
						type: 'warning',
						message: '请扫描正确的物料二维码'
					})
				}
				const array = this.query.reelId.split('*')
				if (array.length !== 6) {
					this.query.reelId = ''
					return this.$refs.uToast.show({
						type: 'warning',
						message: '请扫描正确的物料二维码'
					})
				}
				// 物料码 批次 日期 供应商 数量 reelId
				this.scanMaterial = {
					materialCode: array[0],
					materialBatch: array[1],
					date: array[2],
					supplier: array[3],
					materialQty: Number(array[4]),
					reelId: array[5]
				}
				this.query.reelId = this.scanMaterial.reelId
			}
		}
	}
</script>

<style lang="scss">
.commonButton {
	width: 100%;
	display: flex;
	gap: 20rpx;
	position: fixed;
	left: 0;
	bottom: 0;
	box-sizing: border-box;
	padding: 15rpx;
	justify-content: space-between;
}
</style>
